package com.sailfish.springbootdemo.service.db1;

import com.sailfish.springbootdemo.dao.db1.Test1Dao;
//import com.sailfish.springbootdemo.dao.db2.Test2Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class TestService {

    @Autowired
    private Test1Dao testDao1;

//    @Autowired
//    private Test2Dao testDao2;

    @Qualifier("MysqlTransactionManager1")
    @Autowired
    private DataSourceTransactionManager transactionManager1;

    @Qualifier("MysqlTransactionManager2")
    @Autowired
    private DataSourceTransactionManager transactionManager2;

    /**
     * 测试数据源1事务 [通过@Transactional注解，注意使用try catch，捕获到异常不会自动回滚，在catch中添加 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 即可]
     *
     * @return
     */
    @Transactional(transactionManager = "MssqlTransactionManager1")
    public void TestInsertUser1() {
        // 获取当前事务
        try {
            /*User u1 = new User();
            String uuid = UUID.randomUUID().toString();
            u1.setPassword(uuid);
            u1.setUserId(uuid);
            u1.setUserName(uuid);
            testDao1.TestInsertUser1(u1);
            int a = 1/0;
            u1 = new User();
            uuid = UUID.randomUUID().toString();
            u1.setPassword(uuid);
            u1.setUserId(uuid);
            u1.setUserName(uuid);*/
            //testDao1.TestInsertUser1(u1);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
    }

    /**
     * 测试数据源2事务 [手动开启事务和回滚，不要使用 @Transactional 注解，否则多个事务会导致冲突]
     *
     * @return
     */
    public void TestInsertUser2() {
        // 获取当前事务
        TransactionStatus status = transactionManager2.getTransaction(new DefaultTransactionDefinition());
        /*try{
            User u1 = new User();
            String uuid = UUID.randomUUID().toString();
            u1.setPassword(uuid);
            u1.setUserId(uuid);
            u1.setUserName(uuid);
//            testDao2.TestInsertUser2(u1);
            int a = 1/0;
            u1 = new User();
            uuid = UUID.randomUUID().toString();
            u1.setPassword(uuid);
            u1.setUserId(uuid);
            u1.setUserName(uuid);
//            testDao2.TestInsertUser2(u1);
            transactionManager2.commit(status);
        }catch (Exception e){
            System.out.println(e.getMessage());
            transactionManager2.rollback(status);
        }*/
    }
}
